package com.g2room.driving.grap.life;

import java.io.IOException;
import java.net.MalformedURLException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Tag;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSON;
import com.g2room.driving.grap.GenericGrap;
import com.g2room.driving.grap.life.model.po.GrapErr;
import com.g2room.driving.util.DateUtil;
import com.g2room.myuall.grap.univ.po.LifeNews;
import com.g2room.myuall.util.DbUtil;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;

public class GrapWlkstData {
	//

	String baseUrl = "http://www.wlkst.com";
	public static void main(String[] args) {
		GrapWlkstData cxd = new GrapWlkstData();
////		cxd.getList("http://www.wlkst.com/shcs/index.jhtml");
////		cxd.getList("http://www.wlkst.com/mzfq/index.jhtml");
//		for(int i=1; i< 32; i++)
//		cxd.getList("http://search.tianya.cn/bbs?q=重庆城的老龙门阵&pn="+i);
		
		cxd.handleContent();
	}
	
	public void handleContent() {
		List<LifeNews> lns = getContent();
		for(LifeNews ln : lns) {
			Document doc = GenericGrap.getDocByHtml(ln.getContent());
//			doc.select("font").remove();
			Elements els = doc.select("a");
			if(null == els || els.isEmpty()) continue;
			for(Element el : els) {
				el.attr("href", "#");
//				el = new Element(new Tag("span"), el.text());
			}
//			doc.select("img").remove();
			updateArti(doc.body().html(), ln.getId());
//			updateArti(ln.getContent().replaceAll("src=\"/u", "src=\"http://www.wlkst.com/u"), ln.getId());
		}
	}
	
	/**
	 * 获取头部导航菜单
	 * 
	 * @param url
	 * @throws FailingHttpStatusCodeException
	 * @throws MalformedURLException
	 * @throws IOException
	 */
	public void getList(String url) {
		Document doc = null;
		try {
			doc = GenericGrap.getDocByIp(url);
		} catch (IOException e) {
			GrapErr ge = new GrapErr();
			ge.setUrl(url);
			ge.setType("11");
			ge.setContent("获取链接错误");
			ge.setDescription("获取Type错误：" + e.getMessage());
			insert2ErrDb(ge);
			return;
		}

		if (null == doc) return;
		List<String> durls = new ArrayList<String>();
		try {
			Elements els = doc.select(".searchListOne h3 a");
			if (els == null || els.size() == 0) return;
			for (Element el : els) {
//				if(el.text().contains("[民间传说]")) continue;
//				durls.add(baseUrl + el.attr("href"));
				durls.add(el.attr("href"));
			}
		} catch (Exception e) {
			GrapErr ge = new GrapErr();
			ge.setUrl(url);
			ge.setType("11");
			ge.setContent("获取内容错误");
			ge.setDescription("获取Type错误：" + e.getMessage());
			insert2ErrDb(ge);
			return;
		}
		if(durls.size() == 0) return;
		
		for (String u: durls) {
			getDetail(u);
		}
		
	}
	

	public void getDetail(String url) {
		Document doc = null;
		try {
			doc = GenericGrap.getDocByIp(url);
		} catch (Exception e) {
			GrapErr ge = new GrapErr();
			ge.setUrl(url);
			ge.setType("2");
			ge.setContent("");
			ge.setDescription("获取Detail 链接错误：" + e.getMessage());
			insert2ErrDb(ge);
			return;
		}
		LifeNews ln = new LifeNews();
		Elements els = doc.select(".s_title");
		if(null != els && !els.isEmpty()) {
			ln.setTitle(els.first().text().replace("【重庆城的老龙门阵】", ""));
		}
		els = doc.select(".bbs-content");
		if(null != els && !els.isEmpty()) {
			ln.setContent(els.first().html());
			ln.setSummary(els.first().text().substring(0, Math.min(200, els.first().text().length())));
		}
		ln.setTypeId(1);
		ln.setTypeName("民间传说");
		ln.setSource("天涯");
		ln.setSourceUrl(url);
		insert2FoodDoDb(ln);
	}

	public void insert2FoodDoDb(LifeNews ln) {
		String sql = "insert into life_arti(content,title,summary,type_id,type_name,source,source_url) values(?,?,?,?,?,?,?)";
		PreparedStatement ps = null;
		try {
			ps = DbUtil.getConn("life").prepareStatement(sql);
				ps.setString(1, getStr(ln.getContent()));
				ps.setString(2, getStr(ln.getTitle()));
				ps.setString(3, getStr(ln.getSummary()));
				ps.setInt(4, ln.getTypeId() == null ? 0 : ln.getTypeId());
				ps.setString(5, getStr(ln.getTypeName()));
				ps.setString(6, getStr(ln.getSource()));
				ps.setString(7, getStr(ln.getSourceUrl()));
			ps.execute();
//			try {
//				// 插入到索引
//				new Index().createIndexBatch(ids, idxNis, IndexType.NEWS.val());
//				refreshIndex();
//			} catch (Exception e) {
//				GrapErr ge = new GrapErr();
//				ge.setType("6");
//				ge.setContent(JSON.toJSONString(nis));
//				ge.setDescription("Index错误：" + e.getMessage());
//				insert2ErrDb(ge);
//				return;
//			}
		} catch (Exception e) {
			e.printStackTrace();
			GrapErr ge = new GrapErr();
			ge.setType("5");
			ge.setContent(JSON.toJSONString(ln));
			ge.setDescription("insert DB 错误：" + e.getMessage());
			insert2ErrDb(ge);
			return;
		} finally {
			if (null != ps) {
				try {
					ps.close();
				} catch (SQLException e) {
				}
			}
		}
	}
	

	
	private String getStr(String str) {
		try {
			str = null == str ? null : str.replaceAll("[\\ud83c\\udc00-\\ud83c\\udfff]|[\\ud83d\\udc00-\\ud83d\\udfff]|[\\u2600-\\u27ff]", "");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

	public void insert2ErrDb(GrapErr ge) {
		PreparedStatement grapPs = null;
		try {
			grapPs = DbUtil.getConn("life").prepareStatement("insert into grap_err(description,url,content,type,create_time) values(?,?,?,?,?)");
			grapPs.setString(1, ge.getDescription());
			grapPs.setString(2, ge.getUrl());
			grapPs.setString(3, ge.getContent());
			grapPs.setString(4, ge.getType());
			grapPs.setString(5, DateUtil.date2String(new Date(), "yyyy-MM-dd HH:mm:ss"));
			grapPs.execute();
			grapPs.close();
		} catch (Exception e) {

		} finally {
			if (null != grapPs) {
				try {
					grapPs.close();
				} catch (SQLException e) {

				}
			}
		}
	}
	


	public void updateArti(String content, int id) {
		PreparedStatement grapPs = null;
		try {
			grapPs = DbUtil.getConn("life").prepareStatement("update life_food_do set content=? where id=?");
			grapPs.setString(1, content);
			grapPs.setInt(2, id);
			grapPs.execute();
			grapPs.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != grapPs) {
				try {
					grapPs.close();
				} catch (SQLException e) {

				}
			}
		}
	}
	
	public static List<LifeNews> getContent() {
		List<LifeNews> univs = new ArrayList<LifeNews>();
		try {
			Connection connection = DbUtil.getConn("life");
			Statement statement = connection.createStatement();
			String sql = "select * from life_food_do ";
			ResultSet rs = statement.executeQuery(sql);
			while(rs.next()) {
				LifeNews univ = new LifeNews();
				univ.setId(rs.getInt("id"));
				univ.setTitle(rs.getString("title"));
				univ.setContent(rs.getString("content"));
				univs.add(univ);
			}
			rs.close();
			statement.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
		return univs;
	}
}
